<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>文章信息</span>
          <el-button style="float: right; margin-left: 10px" type="primary" @click="submitForm">保存</el-button>
          <el-button style="float: right" @click="goBack">返回</el-button>
        </div>
        
        <el-row>
            <el-form-item label="所属栏目" prop="categoryId" style="width: 40%">
              <el-cascader
                v-model="form.categoryId"
                placeholder="请选择所属栏目"
                :options="categoryOptions"
                :props="{ value: 'id', label: 'categoryTitle', children: 'children', checkStrictly: true, emitPath: false }"
                style="width: 100%"
              ></el-cascader>
            </el-form-item>

          
        </el-row>

        <el-row>
         
            <el-form-item label="文章标题" prop="contentTitle" style="width: 50%">
              <el-input v-model="form.contentTitle" placeholder="请输入文章标题" />
            </el-form-item>
 
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="文章副标题" prop="contentShortTitle">
              <el-input v-model="form.contentShortTitle" placeholder="请输入文章副标题" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="关键字" prop="contentKeyword">
              <el-input v-model="form.contentKeyword" placeholder="请输入关键字" />
            </el-form-item>
          </el-col> -->
        </el-row>

        <!-- <el-form-item label="文章描述" prop="contentDescription">
          <el-input v-model="form.contentDescription" type="textarea" placeholder="请输入文章描述" />
        </el-form-item> -->

        

        <el-form-item label="文章内容" prop="contentDetails">
          <Editor v-model="form.contentDetails" :min-height="192"/>
        </el-form-item>

        <el-form-item label="文章缩略图" prop="contentImg">
          <ImageUpload v-model="form.contentImg" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="文章来源" prop="contentSource">
              <el-input v-model="form.contentSource" placeholder="请输入文章来源" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="文章作者" prop="contentAuthor">
              <el-input v-model="form.contentAuthor" placeholder="请输入文章作者" />
            </el-form-item>
          </el-col> -->
        </el-row>

        <el-row>
          <el-col :span="4">
            <el-form-item label="显示状态" prop="contentDisplay">
              <el-select v-model="form.contentDisplay" placeholder="请选择显示状态" style="width: 100%">
                <el-option label="不显示" value="0"></el-option>
                <el-option label="显示" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="自定义顺序" prop="contentSort">
              <el-input-number v-model="form.contentSort" controls-position="right" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="文章标签" prop="contentTags">
          <el-input v-model="form.contentTags" placeholder="请输入文章标签，多个标签用逗号分隔" />
        </el-form-item> -->

        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="点击次数" prop="contentHit">
              <el-input-number v-model="form.contentHit" controls-position="right" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章链接" prop="contentOutLink">
              <el-input v-model="form.contentOutLink" placeholder="请输入文章链接" />
            </el-form-item>
          </el-col>
        </el-row> -->

        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="删除标记" prop="del">
              <el-select v-model="form.del" placeholder="请选择删除标记" style="width: 100%">
                <el-option label="正常" value="0"></el-option>
                <el-option label="已删除" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { addContent } from "@/api/cms/content"
import { listTreeCategory } from "@/api/cms/category"
import ImageUpload from "@/components/ImageUpload"
import Editor from "@/components/Editor"

export default {
  name: "ContentAdd",
  components: { ImageUpload, Editor },
  data() {
    return {
      // 分类树形选项
      categoryOptions: [],
      // 表单参数
      form: {
        categoryId: null,
        contentDetails: null,
        contentTitle: null,
        contentShortTitle: null,
        contentKeyword: null,
        contentDescription: null,
        contentImg: null,
        contentSource: null,
        contentAuthor: null,
        contentDisplay: "1",
        contentSort: 0,
        contentTags: null,
        contentHit: 0,
        contentOutLink: null,
        del: "0"
      },
      // 表单校验
      rules: {
        categoryId: [{ required: true, message: "所属栏目不能为空", trigger: "change" }],
        contentTitle: [{ required: true, message: "文章标题不能为空", trigger: "blur" }],
        contentDetails: [{ required: true, message: "文章内容不能为空", trigger: "blur" }]
      }
    }
  },
  created() {
    this.getCategoryTree()
  },
  methods: {
    /** 获取分类树形结构 */
    getCategoryTree() {
      listTreeCategory().then(response => {
        this.categoryOptions = response.data || []
      })
    },
    
    /** 返回按钮 */
    goBack() {
      this.$router.push("/cms/content");
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 处理表单数据，确保富文本内容正确编码
          const submitForm = { ...this.form }
          
          addContent(submitForm).then(response => {
            this.$modal.msgSuccess("新增成功")
            this.goBack()
          }).catch(error => {
            console.error("新增失败:", error)
            this.$modal.msgError("新增失败: " + (error.message || "未知错误"))
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.box-card {
  margin-bottom: 20px;
}
</style>